const express = require("express");
const router = express.Router();
const handle = require("../routes_handle/company");
const { Response } = require("../utils/response");

// 创建或更新公司信息
router.post("/company/save", async (req, res, next) => {
  try {
    const { companyId, name, industry, address, userId, logo } = req.body;
    if (!name || !industry || !address || !userId) {
      return res.json(Response.error("缺少必要参数"));
    }

    const result = await handle.createOrUpdateCompany({
      companyId,
      name,
      industry,
      address,
      userId,
      logo,
    });
    res.json(Response.success(result, "操作成功"));
  } catch (err) {
    next(err);
  }
});

// 获取公司信息
router.post("/company/info", async (req, res, next) => {
  try {
    const { id } = req.body;
    if (!id) {
      return res.json(Response.error("缺少公司ID"));
    }

    const result = await handle.getCompanyInfo(id);
    res.json(Response.success(result, "获取成功"));
  } catch (err) {
    next(err);
  }
});

// 签到
router.post("/company/checkin", async (req, res, next) => {
  try {
    const { companyId, userId, date } = req.body;
    if (!companyId || !userId || !date) {
      return res.json(Response.error("缺少必要参数"));
    }

    const result = await handle.checkin({ companyId, userId, date });
    res.json(Response.success(result, "签到成功"));
  } catch (err) {
    next(err);
  }
});

// 签到记录查询
router.post("/checkin/list", async (req, res, next) => {
  try {
    const { companyId, userId, date } = req.body;
    const result = await handle.getCheckinList({ companyId, userId, date });
    res.json(Response.success(result, "获取成功"));
  } catch (err) {
    next(err);
  }
});

// 获取公司列表
router.post("/company/list", async (req, res, next) => {
  try {
    const { name } = req.body;
    const result = await handle.getCompanyList({ name });
    res.json(Response.success(result, "获取成功"));
  } catch (err) {
    next(err);
  }
});

module.exports = router;
